package cn.tedu.mall.search.service.impl;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.pojo.product.model.Spu;
import cn.tedu.mall.pojo.search.entity.SpuEntity;
import cn.tedu.mall.pojo.search.entity.SpuForElastic;
import cn.tedu.mall.product.service.front.IForFrontSpuService;
import cn.tedu.mall.search.repository.SpuSearchRepository;
import cn.tedu.mall.search.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private SpuSearchRepository spuSearchRepository;

    @Override
    public JsonPage<SpuEntity> search(String keyword, Integer page, Integer pageSize) {
        if (page == null){
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        log.debug("page:{}",page);
        log.debug("pageSize:{}",pageSize);
        Page<SpuEntity> spuEntities = spuSearchRepository
                .querySearchByText(keyword, PageRequest.of(page-1,pageSize));
        JsonPage jsonPage = new JsonPage();
        jsonPage.setPage(page);
        jsonPage.setPageSize(pageSize);
        // 赋值总页数
        jsonPage.setTotalPage(spuEntities.getTotalPages());
        // 赋值总条数
        jsonPage.setTotal(spuEntities.getTotalElements());
        // 赋值数据
        jsonPage.setList(spuEntities.getContent());

        return jsonPage;
    }

    @Override
    public void loadSpuByPage() {

    }
}
